package commonClasses;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;

public class Legend {
	public static void main(String[] args) throws IOException {

		FileReader f = new FileReader(
				"J:\\BC3_IMT_Training\\Journal name and lines.txt");
		FileReader fr = null;
		BufferedReader fb = new BufferedReader(f);
		String name = fb.readLine();
		while (name != null) {

			String[] split = name.split("\t");

			PrintStream ps = new PrintStream("J:\\BC3_IMT_Training\\Sections\\"
					+ split[0] + "\\" + split[0] + " - Legends.txt");
			File file = new File("J:\\BC3_IMT_Training\\text per page\\"
					+ split[0]);
			int count = file.listFiles().length;
			if(count!=1)
			{
				for(int i=1;i<=count;i++){
					fr = new FileReader("J:\\BC3_IMT_Training\\text per page\\"+split[0]+"\\"+i+".txt");

					BufferedReader fbr = new BufferedReader(fr);
					searchLegand(fbr,ps);

				}
			}

			else{
				fr = new FileReader("J:\\BC3_IMT_Training\\text per page\\"+split[0]+"\\"+split[0]+".txt");
				BufferedReader fbr = new BufferedReader(fr);
				searchLegand(fbr,ps);


			}
			System.out.println("Done");
			name=fb.readLine();
		}
	}

	static void searchLegand(BufferedReader fbr, PrintStream ps ) throws IOException{
		String str = fbr.readLine();
		while (str != null) {
				if (str.startsWith("FIGURE")
						|| str.startsWith("FIGURE")
						|| str.startsWith("Figure")
						|| str.startsWith("Fig.")
						|| str.startsWith("FIG.")
						|| str.startsWith("TABLE")
						|| str.startsWith("Table")) {
					String s = str;
					ps.println();
					ps.println();
					str=fbr.readLine();
					while(str.equals("Paragraph End")==false){
						if(s.endsWith("-")){
							s=s+str;
						}
						else{
						s=s+" "+str;
						}
						str=fbr.readLine();
					}
					System.out.println(s);
					ps.println(s);
				}
				else
				str=fbr.readLine();
			}
			
		}
}